package demo.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;

/**
 * @see demo.javax.filter.FilterDemo
 * @see org.springframework.web.filter.CharacterEncodingFilter
 *
 */
public class OncePerRequestFilterDemo extends OncePerRequestFilter {

    private static Logger logger = LoggerFactory.getLogger(OncePerRequestFilterDemo.class);

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        logger.info("在DispatcherServlet之前执行");
        String contextPath = request.getContextPath();
        String servletPath = request.getServletPath();
        String requestUri = request.getRequestURI();
        StringBuffer requestUrl = request.getRequestURL();
        logger.info("contextPath:{}, servletPath:{}, requestUri:{},requestUrl:{}", contextPath, servletPath, requestUri,
                requestUrl);
        filterChain.doFilter(request, response);
        logger.info("在视图页面返回给客户端之前执行，但是执行顺序在Interceptor之后执行");
    }
}
